package camuyen.ltwlibrary.util;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import camuyen.ltwlibrary.dao.AuthorDao;
import camuyen.ltwlibrary.dao.BookDao;
import camuyen.ltwlibrary.dao.CategoryDao;
import camuyen.ltwlibrary.dao.UserDao;
import camuyen.ltwlibrary.model.Author;
import camuyen.ltwlibrary.model.Book;
import camuyen.ltwlibrary.model.Category;
import camuyen.ltwlibrary.model.User;

public class Bootstrap implements ServletContextListener {
	public void contextInitialized(ServletContextEvent servletContextEvent) {
		// Generate database schema
		HibernateUtil.openSession().close();
		// Create test user
		User camUyen = new User();
		camUyen.setUsername("camuyen");
		camUyen.setPassword("1");
		UserDao.saveUser(camUyen);

		// Create category
		CategoryDao.saveCategory(new Category("Children"));
		CategoryDao.saveCategory(new Category("Manga"));
		CategoryDao.saveCategory(new Category("Fiction"));
		CategoryDao.saveCategory(new Category("Science"));
		CategoryDao.saveCategory(new Category("History"));

		Author a1 = new Author("J.R.R. Tolkien");
		Author a2 = new Author("Thanhtaa");
		List<Author> authors = new ArrayList<Author>();
		authors.add(a1);
		authors.add(a2);
		AuthorDao.saveAuthor(a1);
		AuthorDao.saveAuthor(a2);

		String s1 = "Sauron, the Dark Lord, has gathered to him the Rings of Power - the means by which he will be able to rule the world. All he lacks in his plan for domination is the Ruling Ring, which has fallen into the hands of the hobbit Bilbo Baggins.";
		String s2 = "The Fellowship of the Ring is the first of three volumes of the epic novel The Lord of the Rings by the English author J. R. R. Tolkien.";

		BookDao.saveBook(new Book("The Lord of the Rings", authors, s1));
		BookDao.saveBook(new Book("The Fellowship of the Ringl", authors, s2));

		// Fiction Book Lists
		Author af1 = new Author("Jane Austen");
		Author af2 = new Author("J.K. Rowling");
		Author af3 = new Author("George Orwell");
		Author af4 = new Author("Anna Frank");
		Author af5 = new Author("Harper Lee");
		List<Author> authorF1 = new ArrayList<Author>();
		List<Author> authorF2 = new ArrayList<Author>();
		List<Author> authorF3 = new ArrayList<Author>();
		List<Author> authorF4 = new ArrayList<Author>();
		List<Author> authorF5 = new ArrayList<Author>();
		authorF1.add(af1);
		authorF2.add(af2);
		authorF3.add(af3);
		authorF4.add(af4);
		authorF5.add(af5);

		AuthorDao.saveAuthor(af1);
		AuthorDao.saveAuthor(af2);
		AuthorDao.saveAuthor(af3);
		AuthorDao.saveAuthor(af4);
		AuthorDao.saveAuthor(af5);

		BookDao.saveBook(new Book("Pride and Prejudece", authorF1, "no summary"));
		BookDao.saveBook(new Book("Harry Potter and the Sorcerer's Stone",
				authorF2, "no summary"));
		BookDao.saveBook(new Book("1984", authorF3, "no summary"));
		BookDao.saveBook(new Book("The Diary of a Young Girl", authorF4,
				"no summary"));
		BookDao.saveBook(new Book("To Kill a Mockingbird", authorF5,
				"no summary"));

		// Science Book Lists
		Author as1 = new Author("Bill Bryson");
		Author as2 = new Author("Stephen Hawking");
		Author as3 = new Author("Carl Sagan");
		Author as4 = new Author("Richard Dawking");
		Author as5 = new Author("Jared Diamond");
		List<Author> authorS1 = new ArrayList<Author>();
		List<Author> authorS2 = new ArrayList<Author>();
		List<Author> authorS3 = new ArrayList<Author>();
		List<Author> authorS4 = new ArrayList<Author>();
		List<Author> authorS5 = new ArrayList<Author>();
		authorS1.add(as1);
		authorS2.add(as2);
		authorS3.add(as3);
		authorS4.add(as4);
		authorS5.add(as5);

		AuthorDao.saveAuthor(as1);
		AuthorDao.saveAuthor(as2);
		AuthorDao.saveAuthor(as3);
		AuthorDao.saveAuthor(as4);
		AuthorDao.saveAuthor(as5);

		BookDao.saveBook(new Book("A Short History of Nearly Everything",
				authorS1, "no summary"));
		BookDao.saveBook(new Book("A Brief History of Time", authorS2,
				"no summary"));
		BookDao.saveBook(new Book("Cosmos", authorS3, "no summary"));
		BookDao.saveBook(new Book("The Selfish", authorS4, "no summary"));
		BookDao.saveBook(new Book(
				"Guns, Germs, and Steel: The Fates of Human Societies",
				authorS5, "no summary"));

		// History Book Lists
		Author ah1 = new Author("Charles Drawin");
		Author ah2 = new Author("Anonymous");
		/** Author ah3 = new Author("Anonymous"); */
		Author ah4 = new Author("Karl Marx");
		Author ah5 = new Author("Plato");
		List<Author> authorH1 = new ArrayList<Author>();
		List<Author> authorH2 = new ArrayList<Author>();
		/** List<Author> authorH3 = new ArrayList<Author>(); */
		List<Author> authorH4 = new ArrayList<Author>();
		List<Author> authorH5 = new ArrayList<Author>();
		authorH1.add(ah1);
		authorH2.add(ah2);
		/** authorH3.add(ah3); */
		authorH4.add(ah4);
		authorH5.add(ah5);

		AuthorDao.saveAuthor(ah1);
		AuthorDao.saveAuthor(ah2);
		/** AuthorDao.saveAuthor(ah3); */
		AuthorDao.saveAuthor(ah4);
		AuthorDao.saveAuthor(ah5);

		BookDao.saveBook(new Book("The Origin of Species", authorH1,
				"no summary"));
		BookDao.saveBook(new Book("Holy Bible: King James Version", authorH2,
				"no summary"));
		BookDao.saveBook(new Book("The Quran", authorH2, "no summary"));
		BookDao.saveBook(new Book("The Communist Manifesto", authorH4,
				"no summary"));
		BookDao.saveBook(new Book("The Republic", authorH5, "no summary"));

		// Manga
		Author am1 = new Author("Masashi Kishimoto");
		Author am2 = new Author("Rumiko Takahashi");
		Author am3 = new Author("Naoko Takeuchi");
		Author am4 = new Author("Clamp");
		Author am5 = new Author("Nobuhiro Watsuki");
		List<Author> authorM1 = new ArrayList<Author>();
		List<Author> authorM2 = new ArrayList<Author>();
		List<Author> authorM3 = new ArrayList<Author>();
		List<Author> authorM4 = new ArrayList<Author>();
		List<Author> authorM5 = new ArrayList<Author>();
		authorM1.add(am1);
		authorM2.add(am2);
		authorM3.add(am3);
		authorM4.add(am4);
		authorM5.add(am5);

		AuthorDao.saveAuthor(am1);
		AuthorDao.saveAuthor(am2);
		AuthorDao.saveAuthor(am3);
		AuthorDao.saveAuthor(am4);
		AuthorDao.saveAuthor(am5);

		BookDao.saveBook(new Book("Naruto", authorM1, "no summary"));
		BookDao.saveBook(new Book("InuYasha", authorM2, "no summary"));
		BookDao.saveBook(new Book("Sailor moon", authorM3, "no summary"));
		BookDao.saveBook(new Book("Tsubasa: RESERVoir CHRoNiCLE", authorM4,
				"no summary"));
		BookDao.saveBook(new Book("Rurouni Kenshin", authorM5, "no summary"));

		// Children
		Author ac1 = new Author("E.B White");
		Author ac2 = new Author("France Hodgson Burnett");
		Author ac3 = new Author("C.S Lewis");
		Author ac4 = new Author("Lousia May Alcott");
		Author ac5 = new Author("Roald Dalh");
		List<Author> authorC1 = new ArrayList<Author>();
		List<Author> authorC2 = new ArrayList<Author>();
		List<Author> authorC3 = new ArrayList<Author>();
		List<Author> authorC4 = new ArrayList<Author>();
		List<Author> authorC5 = new ArrayList<Author>();
		authorC1.add(ac1);
		authorC2.add(ac2);
		authorC3.add(ac3);
		authorC4.add(ac4);
		authorC5.add(ac5);

		AuthorDao.saveAuthor(ac1);
		AuthorDao.saveAuthor(ac2);
		AuthorDao.saveAuthor(ac3);
		AuthorDao.saveAuthor(ac4);
		AuthorDao.saveAuthor(ac5);

		BookDao.saveBook(new Book("Charlotte's", authorC1, "no summary"));
		BookDao.saveBook(new Book("The Secret Garden", authorC2, "no summary"));
		BookDao.saveBook(new Book(
				"The Lion, the Witch, and the Wardrobe (Choronicles of Narnia)",
				authorC3, "no summary"));
		BookDao.saveBook(new Book("Little Women", authorC4, "no summary"));
		BookDao.saveBook(new Book("Matilda", authorC5, "no summary"));
	}

	public void contextDestroyed(ServletContextEvent servletContextEvent) {
        HibernateUtil.closeSessionFactory();
	}
}
